package com.example.basic.utils;

import com.example.basic.exception.ServiceException;

import java.util.Random;

/**
 * 自动生成编码
 */
public class CodeGen {

    public static String generateCode(int count) {

        if(count <= 0){
            throw new ServiceException("编码长度必须大于0");
        }

        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        StringBuilder codeBuilder = new StringBuilder();

        Random random = new Random();

        for (int i = 0; i <count ; i++) {
            int index = random.nextInt(characters.length());
            char codeChar = characters.charAt(index);
            codeBuilder.append(codeChar);
        }

        return codeBuilder.toString();
    }
}
